{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ab51749f",
   "metadata": {},
   "source": [
    "------\n",
    "* 时间：2022-10-24/week10\n",
    "* Python学习记录\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f3be8e0",
   "metadata": {},
   "source": [
    "# 本周学习目标\n",
    "> 1.while循环  \n",
    "> 2.购物车实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77266e31",
   "metadata": {},
   "source": [
    "## 1.while循环\n",
    "* 回顾：for循环：for + range()可以指定循环的次数-->关于网页URL队列的抓取实践\n",
    "* while循环：当某种条件成立时，停止循环\n",
    "* while True：无限循环/死循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b6e0b7d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "啊对对对 0\n",
      "啊对对对 1\n",
      "啊对对对 2\n",
      "啊对对对 3\n",
      "啊对对对 4\n",
      "啊对对对 5\n",
      "啊对对对 6\n",
      "啊对对对 7\n",
      "啊对对对 8\n",
      "啊对对对 9\n",
      "啊对对对 10\n",
      "啊对对对 11\n",
      "啊对对对 12\n",
      "啊对对对 13\n",
      "啊对对对 14\n",
      "啊对对对 15\n",
      "啊对对对 16\n",
      "啊对对对 17\n",
      "啊对对对 18\n",
      "啊对对对 19\n",
      "啊对对对 20\n",
      "啊对对对 21\n",
      "啊对对对 22\n",
      "啊对对对 23\n",
      "啊对对对 24\n",
      "啊对对对 25\n",
      "啊对对对 26\n",
      "啊对对对 27\n",
      "啊对对对 28\n",
      "啊对对对 29\n",
      "啊对对对 30\n",
      "啊对对对 31\n",
      "啊对对对 32\n",
      "啊对对对 33\n",
      "啊对对对 34\n",
      "啊对对对 35\n",
      "啊对对对 36\n",
      "啊对对对 37\n",
      "啊对对对 38\n",
      "啊对对对 39\n",
      "啊对对对 40\n",
      "啊对对对 41\n",
      "啊对对对 42\n",
      "啊对对对 43\n",
      "啊对对对 44\n",
      "啊对对对 45\n",
      "啊对对对 46\n",
      "啊对对对 47\n",
      "啊对对对 48\n",
      "啊对对对 49\n",
      "啊对对对 50\n",
      "啊对对对 51\n",
      "啊对对对 52\n",
      "啊对对对 53\n",
      "啊对对对 54\n",
      "啊对对对 55\n",
      "啊对对对 56\n",
      "啊对对对 57\n",
      "啊对对对 58\n",
      "啊对对对 59\n",
      "啊对对对 60\n",
      "啊对对对 61\n",
      "啊对对对 62\n",
      "啊对对对 63\n",
      "啊对对对 64\n",
      "啊对对对 65\n",
      "啊对对对 66\n",
      "啊对对对 67\n",
      "啊对对对 68\n",
      "啊对对对 69\n",
      "啊对对对 70\n",
      "啊对对对 71\n",
      "啊对对对 72\n",
      "啊对对对 73\n",
      "啊对对对 74\n",
      "啊对对对 75\n",
      "啊对对对 76\n",
      "啊对对对 77\n",
      "啊对对对 78\n",
      "啊对对对 79\n",
      "啊对对对 80\n",
      "啊对对对 81\n",
      "啊对对对 82\n",
      "啊对对对 83\n",
      "啊对对对 84\n",
      "啊对对对 85\n",
      "啊对对对 86\n",
      "啊对对对 87\n",
      "啊对对对 88\n",
      "啊对对对 89\n",
      "啊对对对 90\n",
      "啊对对对 91\n",
      "啊对对对 92\n",
      "啊对对对 93\n",
      "啊对对对 94\n",
      "啊对对对 95\n",
      "啊对对对 96\n",
      "啊对对对 97\n",
      "啊对对对 98\n",
      "啊对对对 99\n"
     ]
    }
   ],
   "source": [
    "# 1. 指定次数\n",
    "count = 0\n",
    "while True:\n",
    "    print(\"啊对对对\",count)\n",
    "    count = count+1\n",
    "    if count == 100:\n",
    "        break # break 终止循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6a7c33ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：iohwi\n",
      "密码：[3hfoife\n",
      "账号不存在或密码不正确！请重新输入！您还有2次尝试的机会\n",
      "账号：jfwufp8\n",
      "密码：3hy98\n",
      "账号不存在或密码不正确！请重新输入！您还有1次尝试的机会\n",
      "账号：iofqebf\n",
      "密码：iuhqpuhp\n",
      "账号不存在或密码不正确！请重新输入！您还有0次尝试的机会\n",
      "账号：fewlfj\n",
      "密码：mweoj\n",
      "账号不存在或密码不正确！请1小时后再来尝试\n"
     ]
    }
   ],
   "source": [
    "# 2. while True循环，可以让用户重复输入\n",
    "user01 = \"doggie\"\n",
    "password01 = \"20021007\"\n",
    "count = 0 # 计数器\n",
    "while True:\n",
    "    username = input(\"账号：\")\n",
    "    password = input(\"密码：\")\n",
    "    if user01 == username and password01 == password:\n",
    "        print(\"登录成功~欢迎\"+username)\n",
    "        break\n",
    "    else:\n",
    "        if count<3:\n",
    "            print(\"账号不存在或密码不正确！请重新输入！您还有\"+str(2-count)+\"次尝试的机会\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确！请1小时后再来尝试\")\n",
    "            break\n",
    "        count = count+1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dda445df",
   "metadata": {},
   "source": [
    "## 2.购物车项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "535da890",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：100\n",
      "-----商品列表-----\n",
      "商品编号： 0 柠檬水 6元\n",
      "商品编号： 1 蜜桃四季春 10元\n",
      "商品编号： 2 芒果圣代 12元\n",
      "商品编号： 3 葡萄波波奶茶 8元\n",
      "请输入商品编号：3\n",
      "商品购买成功，正在制作中...您的余额还有92元\n",
      "-----商品列表-----\n",
      "商品编号： 0 柠檬水 6元\n",
      "商品编号： 1 蜜桃四季春 10元\n",
      "商品编号： 2 芒果圣代 12元\n",
      "商品编号： 3 葡萄波波奶茶 8元\n"
     ]
    }
   ],
   "source": [
    "# 1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "product_list = [\n",
    "    [\"柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果圣代\",12],\n",
    "    [\"葡萄波波奶茶\",8]\n",
    "]\n",
    "shopping_car =[]\n",
    "Alipay = int(input(\"请输入余额：\"))\n",
    "while True:\n",
    "    print(\"-----商品列表-----\")\n",
    "    for i in range(4):\n",
    "        print(\"商品编号：\",i,product_list[i][0],str(product_list[i][1])+\"元\") # 格式化输出\n",
    "# 2、让用户输入商品编号进行商品的购买。：直接用商品列表的 index 作为商品编号\n",
    "    user_choice = input(\"请输入商品编号：\")\n",
    "# 情景1：用户正在购买，输入商品编号\n",
    "    if user_choice.isdigit():\n",
    "# 情景一：余额大于商品价格\n",
    "        if Alipay >= product_list[int(user_choice)][1]:\n",
    "            Alipay = Alipay - product_list[int(user_choice)][1]\n",
    "            shopping_car.append(product_list[int(user_choice)])\n",
    "            print(\"商品购买成功，正在制作中...您的余额还有%s元\"%(Alipay))\n",
    "# 情景二：余额小于商品价格\n",
    "        else:\n",
    "           print(\"您的余额不足，请更换支付方式~~\")\n",
    "    elif user_choice == \"q\":\n",
    "        print(\"您已购买的商品：\",shopping_car)\n",
    "        print(\"您的余额还有%s，欢迎下次光临~~\"%(Alipay))\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88ca4888",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89988899",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff838e05",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89208c28",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
